package com.unionftech.content_login.bean

import android.os.Parcel
import android.os.Parcelable
import android.text.TextUtils
import com.unionftech.baselibrary.base.BaseApplication
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.ToastUtils
import com.unionftech.common.R.string

data class LoginTokenInfo(
    val access_token: String,
    val expires_in: String,
    val loginSecurityResultResp: RemoteLoginWarning?
) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString().toString(),
        parcel.readString().toString(),
        parcel.readParcelable(RemoteLoginWarning::class.java.classLoader)
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(access_token)
        parcel.writeString(expires_in)
        parcel.writeParcelable(loginSecurityResultResp, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<LoginTokenInfo> {
        override fun createFromParcel(parcel: Parcel): LoginTokenInfo {
            return LoginTokenInfo(parcel)
        }

        override fun newArray(size: Int): Array<LoginTokenInfo?> {
            return arrayOfNulls(size)
        }
    }
}

fun LoginTokenInfo.toastRemoteWarning() {
    loginSecurityResultResp?.also { remoteLoginWarning ->
        val str = DynamicResourceManager.getString(
            BaseApplication.appContext,
            string.MSUE0007
        )
        val sb = StringBuilder()
        if (!TextUtils.isEmpty(remoteLoginWarning.country)) {
            sb.append(remoteLoginWarning.country)
        }
        if (!TextUtils.isEmpty(remoteLoginWarning.province)) {
            sb.append(" ").append(remoteLoginWarning.province)
        }
        if (!TextUtils.isEmpty(remoteLoginWarning.city)) {
            sb.append(" ").append(remoteLoginWarning.city)
        }
        val toastResult = str.replace("<ph1></ph1>", remoteLoginWarning.loginTime)
            .replace("<ph2></ph2>", sb.toString())
            .replace("<ph3></ph3>", remoteLoginWarning.ipAddress)
            .replace("<ph4></ph4>", remoteLoginWarning.deviceType)
        ToastUtils.longToast(toastResult)
    }
}